package meng.study.redis.jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisDemo {

	private final static String redisHost = "127.0.0.1";
	private final static int redisPort = 6379;

	public static void test() {
		Jedis jedis = new Jedis(redisHost, redisPort);
		jedis.set("hello", "redis");
		String name = jedis.get("hello");
		System.out.println(name);
		jedis.close();
	}

	public static void testString() {
		Jedis jedis = getJedisInsitution();

		// 字符串
		jedis.set("name", "redis");
		String name = jedis.get("name");
		System.out.println(name);
		jedis.del("name");
		name = jedis.get("name");
		System.out.println(name);

		// 20 +1+5 -1-7 = 18
		jedis.set("age", "20");
		jedis.incr("age");
		jedis.incrBy("age", 5);
		jedis.decr("age");
		jedis.decrBy("age", 7);
		String age = jedis.get("age");
		System.out.println(age);

		// 字符追加
		jedis.set("address", "AnHui");
		jedis.append("address", " GuoYang");
		String address = jedis.get("address");
		System.out.println(address);

		// 若键不存在,则先赋初始值0，再加1
		jedis.incr("sum");
		String sum = jedis.get("sum");
		System.out.println(sum);

		jedis.close();
	}

	public static void testString2() {
		Jedis jedis = getJedisInsitution();

		/**
		 * @param nxxx
		 *            NX|XX, NX -- Only set the key if it does not already
		 *            exist. XX -- Only set the key if it already exist.
		 * @param expx
		 *            EX|PX, expire time units: EX = seconds; PX = milliseconds
		 * @param time
		 *            expire time in the units of <code>expx</code>
		 * @return Status code reply
		 */
		jedis.set("clazz", "高一10班", "NX", "EX", 10L);
		// 30s后失效

		jedis.close();
	}

	public static void testHash() {
		Jedis jedis = getJedisInsitution();

		// 单一赋值
		jedis.hset("myset", "name", "mengzhang6");
		jedis.hset("myset", "age", "24");
		jedis.hset("myset", "phone", "18365264942");

		// 批量赋值
		Map<String, String> hash = new HashMap<String, String>();
		hash.put("sal", "4000");
		hash.put("address", "AnHui");
		hash.put("id", "20090541");
		jedis.hmset("myset", hash);

		// 获取值
		String myset_name = jedis.hget("myset", "name");
		System.out.println(myset_name);

		// 批量获取
		List<String> vals = jedis.hmget("myset", "age", "id", "name");
		System.out.println(vals);

		// 获取所有
		Map<String, String> vals2 = jedis.hgetAll("myset");
		System.out.println(vals2);

		// 删除
		jedis.hdel("myset", "id", "sal");

		// 是否存在键
		Boolean flag = jedis.hexists("myset", "name");
		System.out.println(flag);
		flag = jedis.hexists("myset", "hexists");
		System.out.println(flag);

		// 键的数量
		Long num = jedis.hlen("myset");
		System.out.println(num);

		// 所有键 所有值
		Set<String> keys = jedis.hkeys("myset");
		List<String> hvals = jedis.hvals("myset");
		System.out.println(keys);
		System.out.println(hvals);

		// 加减
		jedis.hincrBy("myset", "age", 5);
		jedis.hincrBy("myset", "age", -6);// 没有hdecrby

		jedis.close();

	}
	
	public static void testList() {
		Jedis jedis = JedisDemo.getJedisInsitution();

		// 插入list头部
		// jedis.lpush("list2", "1");
		// jedis.lpush("list2", "2", "3", "4", "5");
		/**
		 * 1 lpushx 同lpush具有相同的功能， 都是向redis数据库中list类型的顶部插入值
		 * 
		 * 2 lpushx 只 能对一个存在的list键进行数据插入操作
		 * 
		 * 3 lpushx 每次只能插入一个value
		 * 
		 * 4 如果执行成功 则返回最新的listKeyName中的元素个数。
		 */
		// jedis.lpushx("list2", "6");

		// 插入list尾部
		// long size = jedis.rpush("list2", "2", "3", "4", "5");
		// System.out.println("list2 size " + size);

		// 移出并获取列表的第一个元素
		// String lpop = jedis.lpop("list2");
		// System.out.println(lpop);

		// 移除并获取列表最后一个元素
		// String rpop = jedis.rpop("list2");
		// System.out.println(rpop);

		// System.out.println(jedis.llen("list2"));

		// 通过索引设值，取值
		// System.out.println(jedis.lindex("list2", 2));
		// jedis.lset("list2", 2, "hello");
		// System.out.println(jedis.lindex("list2", 2));

		List<String> list = jedis.lrange("list2", 1, 5);
		System.out.println(list);

		jedis.close();
	}

	private static JedisPool jedisPool = null;
	static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(30);// 最大连接数
		config.setMaxIdle(10);// 最大空闲连接数
		// 获取连接池
		jedisPool = new JedisPool(config, redisHost, redisPort);
	}

	public static Jedis getJedisInsitution() {
		Jedis jedis = jedisPool.getResource();
		return jedis;

		// try {
		// jedis = jedisPool.getResource();
		// jedis.set("name", "jedis");
		// String name = jedis.get("name");
		// System.out.println(name);
		// } catch (Exception e) {
		// e.printStackTrace();
		// } finally {
		// if (jedis != null)
		// jedis.close();
		// if (jedisPool != null)
		// jedisPool.close();
		// }

	}

}
